package cn.simon.controller;

import cn.simon.model.Seller;
import cn.simon.service.SellerService;

import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

import cn.simon.vo.SellerVO;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;

@Controller
@RequestMapping("/seller")
@Log
public class SellerController {
    @Autowired
    private SellerService sellerService;


    /**
     * 查询商家信息
     */
    @GetMapping("/getAllSeller")
    public String sellerList(Model model) {
        List<Seller> list = sellerService.findAll();
        model.addAttribute("list", list);
        return "sellerList";
    }

    /**
     * 查看需要审批的商家
     */
    @GetMapping("/getApproveSeller")
    public String sellerAppList(Model model){
        List<Seller> list=sellerService.getApproveSeller();
        model.addAttribute("appList",list);
        return "getApproveSeller";
    }

    /**
     * 同意商家的申请
     */
    @GetMapping("/agreeSellerApp")
    public String  agreeSellerApp(Integer id){
        sellerService.unFreezeSeller(id);
        return "redirect:/seller/getApproveSeller";
    }

    /**
     * 冻结商家
     */
    @GetMapping("/freezeSeller")
    public  String freezeSeller(Integer id){
        sellerService.freezeSeller(id);
        return "redirect:/seller/getAllSeller";
    }

    /**
     * 激活商家
     */
    @GetMapping("/unFreezeSeller")
    public  String unFreezeSeller(Integer id){
        sellerService.unFreezeSeller(id);
        return "redirect:/seller/getAllSeller";
    }


    @GetMapping("/getSeller")
    public String findSellerId(Integer id, Model model) {
        Seller seller = sellerService.findSellerId(id);
        model.addAttribute("seller", seller);
        return "getSeller";
    }

    @GetMapping("/modSeller")
    public String updateSellerId(Integer id, Model model) {
        Seller seller = sellerService.findSellerId(id);
        model.addAttribute("seller", seller);
        return "modSeller";
    }

    @PostMapping("/modSeller")
    public String updateSellerId(SellerVO sellerVo) {
        Seller seller = new Seller();
        BeanUtils.copyProperties(sellerVo, seller);
        sellerService.updateSellerId(seller);
        return "redirect:/seller/getSeller";
    }

}
